package com.xs.kanmeitu.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 偏好工具类
 */
public class PreferencesUtil {

    private static final String KEY_LOGIN = "LOGIN";
    private static PreferencesUtil instance;
    private final Context context;
    private final SharedPreferences preferences;

    public PreferencesUtil(Context context){
        this.context = context;

        preferences = context.getSharedPreferences("xiaoshuai", Context.MODE_PRIVATE);

    }

    /**
     * 获取偏好设置工具类(单例设计模式)
     * @return
     */
    public synchronized static PreferencesUtil getInstance(Context context) {
        if(instance == null){
            instance = new PreferencesUtil(context.getApplicationContext());
        }
        return instance;
    }

    //如果要保存用户名就在添加下面两个方法
    /**
     * 设置是否登录
     * @param data
     */
    public void setLogin(Boolean data){
        preferences.edit().putBoolean(KEY_LOGIN,data).commit();
    }

    /**
     * 判断是否登录
     * false 默认未登录
     * @return
     */
    public boolean isLogin(){
        return preferences.getBoolean(KEY_LOGIN,false);
    }

}
